import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testrolebinding:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/1_get_rolebinding_list.yaml'))
    def test_get_rolebinding_list(self, rolebindinginfo):
        # 获取rolebinding列表接口-正常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default')
        method = rolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/2_create_rolebinding.yaml'))
    def test_create_rolebinding(self, rolebindinginfo):
        # 创建rolebinding接口-正常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default')
        method = rolebindinginfo['request']['method']
        data = rolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        rolebindingname = req.json()['metadata']['name']
        globals()['rolebindingname'] = rolebindingname
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/3_get_specific_rolebinding.yaml'))
    def test_get_specific_rolebinding(self, rolebindinginfo):
        # 获取指定rolebinding接口-正常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{rolebindingname}', globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/4_update_rolebinding.yaml'))
    def test_update_rolebinding(self, rolebindinginfo):
        # 更新指定rolebinding接口-正常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{rolebindingname}', globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        rolebindinginfo['request']['data'] = globals()['updatereqbody']
        data = rolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/5_update_section_rolebinding.yaml'))
    def test_update_section_rolebinding(self, rolebindinginfo):
        # 部分更新指定rolebinding的接口-正常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{rolebindingname}', globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        data = rolebindinginfo['request']['data']
        header = rolebindinginfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/6_delete_specific_rolebinding.yaml'))
    def test_delete_specific_rolebinding(self, rolebindinginfo):
        # 删除rolebinding接口-正常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{rolebindingname}', globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/1_get_rolebinding_list_exception.yaml'))
    def test_get_rolebinding_list_exception(self, rolebindinginfo):
        # 获取rolebinding列表接口-异常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default')
        method = rolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/2_create_rolebinding_exception.yaml'))
    def test_create_rolebinding_exception(self, rolebindinginfo):
        # 创建rolebinding接口-异常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default')
        method = rolebindinginfo['request']['method']
        data = rolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/3_get_specific_rolebinding_exception.yaml'))
    def test_get_specific_rolebinding_exception(self, rolebindinginfo):
        # 获取指定rolebinding接口-异常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{rolebindingname}', globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/4_update_rolebinding_exception.yaml'))
    def test_update_rolebinding_exception(self, rolebindinginfo):
        # 更新指定rolebinding接口-异常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{rolebindingname}', globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        rolebindinginfo['request']['data'] = globals()['updatereqbody']
        data = rolebindinginfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/5_update_section_rolebinding_exception.yaml'))
    def test_update_section_rolebinding_exception(self, rolebindinginfo):
        # 部分更新指定rolebinding的接口-异常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{rolebindingname}', globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        data = rolebindinginfo['request']['data']
        header = rolebindinginfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.rolebinding
    @pytest.mark.parametrize('rolebindinginfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/rolebinding/6_delete_specific_rolebinding_exception.yaml'))
    def test_delete_specific_rolebinding_exception(self, rolebindinginfo):
        # 删除rolebinding接口-异常
        base_url = datas.baseUrl + Testrolebinding.url_unify
        url = base_url + rolebindinginfo['request']['url'].replace('{namespace}', 'default').replace(
            '{rolebindingname}', globals()['rolebindingname'])
        method = rolebindinginfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == rolebindinginfo['validate'][0]['equals']['status_code']
